package com.eduhzy.ttw.clazz.mvp.presenter;

import android.app.Application;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.clazz.mvp.contract.GroupReviewContract;
import com.eduhzy.ttw.clazz.mvp.model.entity.ClazzScoreData;
import com.eduhzy.ttw.clazz.mvp.model.entity.ReviewGroupData;
import com.eduhzy.ttw.clazz.mvp.model.entity.ReviewStudentData;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.jess.arms.di.scope.FragmentScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


/**
 * @author xubo
 * @date 2018-12-5 15:35:22
 */
@FragmentScope
public class GroupReviewPresenter extends BasePresenter<GroupReviewContract.Model, GroupReviewContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    @Inject
    BaseQuickAdapter<ReviewGroupData, AutoBaseViewHolder> mAdapter;

    @Inject
    public GroupReviewPresenter(GroupReviewContract.Model model, GroupReviewContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }


    public void getTeamList(int classId, int teamType) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("classId", classId);
        map.put("teamType", teamType);
        map.put("teacherId", SPUtils.getInstance().getString(Constants.USERID));
        mModel.getTeamList(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 3))
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                    mRootView.hideLoading();
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<List<ReviewGroupData>>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<List<ReviewGroupData>> commonData) {
                        if (commonData.isResult()) {
                            mRootView.update(commonData.getData());
                        } else {
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        if (t instanceof ConnectException || t instanceof UnknownHostException) {
                            mAdapter.setEmptyView(RxUtil.getEmptyView(mRootView.getActivity(), RxUtil.ErrorCode.no_network));
                        } else {
                            mAdapter.setEmptyView(RxUtil.getEmptyView(mRootView.getActivity(), RxUtil.ErrorCode.load_error));
                        }
                    }
                });
    }

    public void getEvaluateTagList(int classId, int scoreType) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("classId", classId);
        map.put("teacherId", SPUtils.getInstance().getString(Constants.USERID));
        map.put("scoreType", scoreType);
        mModel.getEvaluateTagList(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 3))
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    mRootView.showDialogLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                    mRootView.hideDialogLoading();
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<List<ClazzScoreData>>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<List<ClazzScoreData>> commonData) {
                        if (commonData.isResult()) {
                            mRootView.updateDialog(commonData.getData());
                        } else {
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }
                });

    }

    public void scoreSave(String studentIds, int classId, int tagId, int teamType, int teamId) {
        QMUITipDialog tipDialog = RxUtil.showLoading(mRootView.getActivity(), "正在评分···");
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("classId", classId);
        map.put("studentIds", studentIds);
        map.put("tagId", tagId);
        map.put("teamId", teamId);
        map.put("teacherId", SPUtils.getInstance().getString(Constants.USERID));
        //隐藏下拉刷新的进度条
        mModel.scoreSave(map)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    tipDialog.show();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(tipDialog::dismiss)
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<Map>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<Map> commonData) {
                        mRootView.showMessage(commonData.getMessage());
                        if (commonData.isResult()) {
                            getTeamList(classId,teamType);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                    }
                });
    }

    public void getTeamUser(int teamId) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("teamId", teamId);
        mModel.getTeamUser(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 3))
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    mRootView.showChooseDialogLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                    mRootView.hideChooseDialogLoading();
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<List<ReviewStudentData>>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<List<ReviewStudentData>> commonData) {
                        if (commonData.isResult()) {
                            mRootView.updateChooseDialog(commonData.getData());
                        } else {
                            mRootView.showMessage(commonData.getMessage());
                        }
                    }
                });

    }
}
